const app = getApp()
var util = require("../../../utils/util.js")
const db = wx.cloud.database()
const _ = db.command
const labelDB=db.collection('labelData')
const collegeDB=db.collection('collegeData')
var allisSelected = true   //选择全校以后其他将不会在被选择

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userOpenid: '',
    currentClick:0,//0,
    labelArray:[], //标签 数组
    academyArray:[],//学院数组
    label_opacity:[], //点亮数组
    academy_opacity:[],
    label_selected:[],
    academy_selected:[],
    labelValue:'',
    showView: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("得到的栏目序号：",options.col)
    let that = this
    that.setData({
      currentClick:options.col
    })
    that.setacademyArray("长江大学")
    that.setLabelArray(that.data.currentClick)
  },
    // //添加了此方法  append
    // onShow:function(options){
    //   allisSelected = true
    // },

//通过栏目获取所有标签 
setLabelArray:function(col){
  let that = this
  labelDB.where({
    col:Number(col)
  }).get({
    success:function(res){
      that.setData({
        labelArray:res.data
      })
      console.log("所有标签:",that.data.labelArray)
      let label_length = that.data.labelArray.length
      for(var i=0;i<label_length;i++){
        that.data.label_opacity.push(0.5)
      }
      console.log(that.data.label_opacity)
    }
  })
},

//通过大学获取所有专业
setacademyArray:function(college){
  let that = this
  collegeDB.where({
    college:college
  }).get({
    success:function(res){ 
      that.setData({
        academyArray:res.data[0].allAcademy,
      })
      console.log(college,"的所有专业",that.data.academyArray)
      let academy_length = that.data.academyArray.length
      for(var i=0;i<academy_length;i++){
        that.data.academy_opacity.push(0.5)
      }
      console.log(that.data.academy_opacity)
    }
  })
},

  //点击每一个学院标签
  lighttheAcademy:function(e){
    console.log(e.target.id)
    let itemID = Number(e.target.id)
    let that=this
    let academy_opacity_item =  that.data.academy_opacity[itemID]
    let arraylength = that.data.academy_opacity.length
    if(0==itemID){  //选择全校
      if(0.5 == academy_opacity_item){
        that.data.academy_opacity[itemID] = 1
        //其他熄灭其他选择
        for(var i=1;i<=arraylength;i++){
          that.data.academy_opacity[i] = 0.5
        }
        that.data.academy_selected = [0]
        allisSelected = false
        // that.data.academy_selected.push(Number(itemID))
      }else{ 
        that.data.academy_opacity[itemID] = 0.5
        allisSelected = true
        that.data.academy_selected=[]
        // that.data.academy_selected.pop(Number(itemID))
        // that.data.academy_selected = [1]
      }
    }
    else{ //没有选择全校
      if(allisSelected){
        if(0.5 == academy_opacity_item){
          that.data.academy_opacity[itemID] = 1
          that.data.academy_selected.push(itemID)
        }else{
          that.data.academy_opacity[itemID] = 0.5
          that.data.academy_selected.splice(that.data.academy_selected.findIndex(e => e ===itemID), 1)
        }
    }else{
      wx.showToast({
        title: '你已经选择了全校',
        icon:"none"
      })
    }
  }
  that.setData({
    academy_opacity:that.data.academy_opacity
  })
    console.log(that.data.academy_opacity,that.data.academy_selected)
  },

    //点击每一个标签
    lighttheLabel:function(e){
      console.log(e.target.id)
      let itemID = Number(e.target.id)
      let that=this
      let label_opacity_item =  that.data.label_opacity[itemID]
      if(0.5 == label_opacity_item){
        that.data.label_opacity[itemID] = 1
        that.data.label_selected.push(itemID)
      }else{
        that.data.label_opacity[itemID] = 0.5
        that.data.label_selected.splice(that.data.label_selected.findIndex(e => e === itemID), 1)
      }
      that.setData({
        label_opacity:that.data.label_opacity
      })
      console.log(that.data.label_opacity)
    },

  //点击确定按钮
  clickBtnSure:function(){
    let that = this
    allisSelected = true
    console.log(that.data.currentClick,that.data.academy_selected,that.data.label_selected)
      var pages = getCurrentPages()
      var beforePage = pages[pages.length-2] 
      // beforePage.setData({
      //   btnSelect:"重选标签"
      // })
      beforePage.setReselectBtn()
      beforePage.getselectedLabel(that.data.label_selected)
      beforePage.getselectedAcademy(that.data.academy_selected)
      wx.navigateBack({
      })
  },



  /*添加标签 */
  increaseLabel:function(e){
    this.setData({
      showView:false
    })
   },
  /* 输入对话框相关操作  开始*/
  getLabelValue:function(e){
    console.log("获得的标签值为：",e.detail.value)
    this.setData({
      labelValue:e.detail.value
    })
  },

  clickSureL:function(){
    let that = this
    let Lvalue = that.data.labelValue
    let Lcol = Number(that.data.currentClick)
    let Lnum = 0
    if(Lvalue!=''){
      console.log("点击了确定",Lvalue)
      labelDB.where({
        label: Lvalue,
        col: Lcol
      }).count().then(res => {
        console.log(res.total)
        if(res.total<=0){
          labelDB.where({
            col: Lcol
          }).count({
            success: function(res) {
              console.log(res.total)
              Lnum = res.total
              if(0!=Lnum){
                console.log(Lnum)
                labelDB.add({
                  data: {
                    col:Lcol,
                    label:Lvalue,
                    labelNum:Lnum
                  },
                  success: function(res) {
                    console.log("添加数据成功！",res)
                    that.setLabelArray(Lcol)
                    that.setData({
                      showView:true,
                    })
                  },
                  fail: function(res) {
                    wx.showToast({
                      title: '系统出错',
                      icon:"none"
                    })
                  }
                })
            }
            },
            fail: function(res) {
              wx.showToast({
                title: '系统出错',
                icon:"none"
              })
            }
          })
        }else{
          wx.showToast({
            title: '该标签已存在',
            icon:'none'
          })
        }
      })
      console.log(Lnum)
    }else{
      wx.showToast({
        title: '你输入了空值',
        icon:"none"
      })
    }
  },
  clickCancelL:function(){
      console.log("点击了取消")
      let that = this
      that.setData({
        showView:true
      })
  },
/* 输入对话框相关操作  结束*/
})